package pkg

import (
	"fmt"
	"github.com/smartwalle/alipay/v3"
)

type Payment interface {
	Pay(subject, orderSn, amount string) string
	//Refund()
}
type AliPay struct {
	AppId      string
	PrivateKey string
	NotifyUrl  string
	ReturnUrl  string
}

func NewAliPay() *AliPay {
	return &AliPay{
		AppId:      "9021000140673249",
		PrivateKey: "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCiQRuqPHWE6vF/vnXP49QFZ8ySMgFHXc1kiy6spQwerFeqFb+Uc5G3llXPnnZX0cHcMv3e+ctCuUNTl2MmYw9dCy89N4lOev4ezQJFZHtJspbxQIJ68OLuds645v5KAhp0G7Kr3moDHMk6LwBR29zuSmYt4bTkyRTAiDz9IDwiBGZEyHgZ7egFmuGdD5bk/arClbwESjG7fvqbEDb7prn/UZxrBnP3s2DFydGgLWpo1iDfr3OmbGQ1NIUcZob5hN/pD1A0DS3R/v9WAE9wsrKwKiO+GYUo9BsN+YN+H8k4JKEP1LkLIRqxeny0q9+eGbMHx2EJonMimpfnV//N4PsPAgMBAAECggEAN0nwX2YStaRJfror8xczXFQuhXK8tNDl+GjlErd7FcisZqzah3+KHAECZKkesnYrlRlbIOwFs0rmAkoH40DGL3JsOnP+Odv3VA+Pwo2m5UoR3UuaKsKC0bdyYdd0t0tYMFBv5dj4Tu80MrtpZHuDIUAA6YmL1poelCzYe3jMhNplXiLSg+NBJJUg0jiI1eDGZj737V3s2VYgNyUYCiSEXGjK8R47X0IsVUo7zRLRtsQWiMzDg4By552yixTMGdkgD/QX9xvAhJ7hlt8i5oc9KNe1bCtrsXt1PcceZ7eqSbPUC+KKArgvK7IZIFkG0WFVAuGqfgxQ+TJhzA4rOZzXAQKBgQD321H3bSL8bGhztn1+kYVRqyPvMiCPL69ik7alpn17ypG2gcbsoom1RkOhNIesf45MZFxDdek3/00wM1ypt8lsU65PkSiABlx7BEvXdWmF91TBIQwAfRe1b4+FnCINCY+M0gIT0gqpX1Cp7D0zzO7wBqqeXTgt/za1kiEpn1WR4QKBgQCnlc0RCVm9+eA+Z2dHWBU1qHaiuT4yoFG1UIRbvRrFO+O3k8OJAxz5Il3Hm30JsFsS1RF6ArSHgQzz4BrLKQDOULBIuiTshMxPVppblEY+X+nmz3tigAjjs6PbUpVlOmNmUpCYO2aqIoHOiVAwKyE+HlYPwaiwGNnsKdZSxKsK7wKBgQCEUoaT/aPr90RcYlTlOXNfgmqQzMYtK1wK2KluDlB3wlHAidBVjQDP0k9QxOaZ5Q9nIT1QlqaHyri+vuz9fqz+ND6fI/PnRX4h/s0unfRUHRMWeDocGKyLpJ+OySg0paIJoCSTyFMA0W4TCZgzC3mUkzqLiWDwzYu7q+QggHwCgQKBgDp9xHc/xjzSa/wb5yY1jiGOTOn6scTFw/7+vVOvqxpQDujSABtgJMqnvgefM4MgNIlcKJ9nNk9dMYIlI/TouMlJzF116tFKgGzokzOHiwTWQTM4E56LhY7D8lrodhBA0tSx2JnpkamsJZZKwtwtEYs2117kftYHtngBGjcFQZWtAoGBALmvMXrAdH1Gq/HhnZuYFYfqZZ4L8FH+8xL7f5NUk1qDh/CLd7s99XALxixwPywg5hWicHjZdLFszrXH7HziKSVmtXEuaje8Nyrll52ecye5XTLZ01geVF7VyLq43+M0O2b0XHMKbSlPqJsNdufD0p4yz4hCYNKmEoK2iDUrIAte",
		NotifyUrl:  "http://6b2e1f35.r12.cpolar.top/alipay/callback/",
		ReturnUrl:  "http://www.baidu.com/",
	}
}

func (a *AliPay) Pay(subject, orderSn, amount string) string {
	var privateKey = a.PrivateKey // 必须，上一步中使用 RSA签名验签工具 生成的私钥
	var client, err = alipay.New(a.AppId, privateKey, false)
	if err != nil {
		fmt.Println(err)
		return ""
	}

	var p = alipay.TradeWapPay{}
	p.NotifyURL = a.NotifyUrl // 必须，同步跳转地址
	p.ReturnURL = a.ReturnUrl // 必须，异步通知地址
	p.Subject = subject
	p.OutTradeNo = orderSn
	p.TotalAmount = amount
	p.ProductCode = "QUICK_WAP_WAY"

	url, err := client.TradeWapPay(p)
	if err != nil {
		fmt.Println(err)
	}

	// 这个 payURL 即是用于打开支付宝支付页面的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	var payURL = url.String()
	//fmt.Println(payURL)
	return payURL
}
